package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NSysStationAsphaltSensorBo;
import com.ruoyi.zhgd.domain.vo.NSysStationAsphaltSensorVo;
import com.ruoyi.zhgd.domain.NSysStationAsphaltSensor;
import com.ruoyi.zhgd.mapper.NSysStationAsphaltSensorMapper;
import com.ruoyi.zhgd.service.INSysStationAsphaltSensorService;
import com.baomidou.dynamic.datasource.annotation.DS;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 拌合楼传感器校正Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NSysStationAsphaltSensorServiceImpl implements INSysStationAsphaltSensorService {

    private final NSysStationAsphaltSensorMapper baseMapper;

    /**
     * 查询拌合楼传感器校正
     */
    @Override
    public NSysStationAsphaltSensorVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询拌合楼传感器校正列表
     */
    @Override
    public TableDataInfo<NSysStationAsphaltSensorVo> queryPageList(NSysStationAsphaltSensorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NSysStationAsphaltSensor> lqw = buildQueryWrapper(bo);
        Page<NSysStationAsphaltSensorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询拌合楼传感器校正列表
     */
    @Override
    public List<NSysStationAsphaltSensorVo> queryList(NSysStationAsphaltSensorBo bo) {
        LambdaQueryWrapper<NSysStationAsphaltSensor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NSysStationAsphaltSensor> buildQueryWrapper(NSysStationAsphaltSensorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NSysStationAsphaltSensor> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTemperature() != null, NSysStationAsphaltSensor::getTemperature, bo.getTemperature());
        lqw.eq(bo.getAsphalt() != null, NSysStationAsphaltSensor::getAsphalt, bo.getAsphalt());
        lqw.eq(bo.getGravel1() != null, NSysStationAsphaltSensor::getGravel1, bo.getGravel1());
        lqw.eq(bo.getGravel2() != null, NSysStationAsphaltSensor::getGravel2, bo.getGravel2());
        lqw.eq(bo.getGravel3() != null, NSysStationAsphaltSensor::getGravel3, bo.getGravel3());
        lqw.eq(bo.getGravel4() != null, NSysStationAsphaltSensor::getGravel4, bo.getGravel4());
        lqw.eq(bo.getGravel5() != null, NSysStationAsphaltSensor::getGravel5, bo.getGravel5());
        lqw.eq(bo.getGravel6() != null, NSysStationAsphaltSensor::getGravel6, bo.getGravel6());
        lqw.eq(bo.getPowder1() != null, NSysStationAsphaltSensor::getPowder1, bo.getPowder1());
        lqw.eq(bo.getPowder2() != null, NSysStationAsphaltSensor::getPowder2, bo.getPowder2());
        lqw.eq(bo.getPowder3() != null, NSysStationAsphaltSensor::getPowder3, bo.getPowder3());
        lqw.eq(bo.getRecycledMaterial() != null, NSysStationAsphaltSensor::getRecycledMaterial, bo.getRecycledMaterial());
        return lqw;
    }

    /**
     * 新增拌合楼传感器校正
     */
    @Override
    public Boolean insertByBo(NSysStationAsphaltSensorBo bo) {
        NSysStationAsphaltSensor add = BeanUtil.toBean(bo, NSysStationAsphaltSensor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改拌合楼传感器校正
     */
    @Override
    public Boolean updateByBo(NSysStationAsphaltSensorBo bo) {
        NSysStationAsphaltSensor update = BeanUtil.toBean(bo, NSysStationAsphaltSensor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NSysStationAsphaltSensor entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除拌合楼传感器校正
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
